

import requests, time
from bs4 import BeautifulSoup
import sys
import os#http://www.cnblogs.com/wq242424/p/5803721.html os模块操作全集http://www.runoob.com/python/os-file-methods.html


headers = {
    'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
    ,
    'cookies': '__cfduid=d5b948c1662cc63494cfc207066399ca31495881607; UM_distinctid=15c497ce31e5b7-0fba8e5852bb95-474f0820-144000-15c497ce31f8d8;'}
	
	
'''
配置导入区域

'''	
def lifan_get(url):
	lifan=requests.get(url, headers=headers,timeout=30)
	lifan.encoding = ('gbk')
	#注释 对于网页解码 requests内部默认以ISO-8859-1解码，可以print(res.encoding)得到 可以手动查看网页编码charset 然后转换
	return lifan

def yema(tex):
	txt_g = lifan_get(tex)
	yema=BeautifulSoup(txt_g.text, 'lxml').find('div', class_="pagelink")('a')[-1].text
	return yema

def 章节(url,name,xiaoshuonanme):


	zhangjiesoup=BeautifulSoup(lifan_get(url).text, 'lxml')
	xiaoshuo_ju=zhangjiesoup.find('div',id="content")
	删除行=zhangjiesoup.find('div',id="content").find_all('br')
	f = open(xiaoshuonanme + '.txt', 'a', encoding='utf-8')  # 写入文本文件一点要记得带上编码
	f.write(name + "\n")
	f.close()
	for i in xiaoshuo_ju:
		if i not in 删除行:
			if "<div" not in i:
				f = open(xiaoshuonanme + '.txt', 'a',encoding='utf-8')#写入文本文件一点要记得带上编码
				f.write(str(i)+"\n")
				f.close()




def 创建文件夹(name):
	name = name.strip()#Python strip() 方法用于移除字符串头尾指定的字符（默认为空格）。
	name_cunzai = os.path.exists(os.path.join("D:\资源", name))#链接''"D:\资源"+name变量使其成为一个路径，os.path.exists 是判断该路径是否存在
	if not name_cunzai:
		print('建了一个名字叫做', name, '的文件夹！')
		os.makedirs(os.path.join("D:\资源",name))  # Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。#os.makedirs() 方法用于递归创建目录。像 mkdir(), 但创建的所有intermediate-level文件夹需要包含子目录。
		os.chdir("D:\资源\\" + name)
		return True
	else:
		print('名字叫做', name, '的文件夹已经存在了！')
		os.chdir("D:\资源\\" + name)
		return False








	
def 导航栏(url):
	n=0
	print('我只是个小小的测试功能'+url)
	txt_get=lifan_get(url)#读取笔下文学首页/编码在函数lifan_get内转换编码
	txt_fenlan_text=BeautifulSoup(txt_get.text, 'lxml').find('td', class_='headfont12')('a')#笔下文学首页取出导航栏
	fenlan=0
	for txt_fenlan in txt_fenlan_text:
		fenlan=fenlan+1
		txt_name=txt_fenlan.text
		创建文件夹(txt_name)
		txt_url='http://www.bxwx9.org'+txt_fenlan['href']#每个分栏的地址
		zizeng_fenlan=0
		for zizeng_fenlan in range(1,int(yema(txt_url))):
			fenlan_url='http://www.bxwx9.org/bsort'+str(fenlan)+'/0/'+str(zizeng_fenlan)+'.htm'
			fenlan_get=lifan_get(fenlan_url)
			fenlan_soup=BeautifulSoup(fenlan_get.text, 'lxml').find('table', class_='grid').find_all('tr')
			for i in fenlan_soup:
				try:

					xiaoshuo_name=i.find_all('td')[0].text
					xiaoshuo_url=i.find_all('td')[1].find('a')['href']
					xiaoshuo_zuozhe=i.find_all('td')[2].text
					xiaoshuo_daxiao=i.find_all('td')[3].text
					xiaoshuo_gengxinshijian=i.find_all('td')[4].text
					xiaoshuozhuangtai=i.find_all('td')[5].text
					print(xiaoshuo_name+xiaoshuo_url+xiaoshuo_zuozhe+xiaoshuo_daxiao)
					xiaoshuo_get = lifan_get(xiaoshuo_url)
					xiaoshuo_soup = BeautifulSoup(xiaoshuo_get.text, 'lxml').find_all('dl')

					for i1 in xiaoshuo_soup:
						zhangjie=[]
						zhangjie_1 = []
						zhangjie_2 = []
						for i2 in i1.find_all('a'):
							zhangjie_1.append(i2)
							a1=0
						for i3 in zhangjie_1:
							a1=a1+1
							zhangjie_2 .append(i3)
							if a1==4:
								zhangjie.append(zhangjie_2[1])
								zhangjie.append(zhangjie_2[3])
								zhangjie.append(zhangjie_2[2])
								zhangjie.append(zhangjie_2[0])
								zhangjie_2 =[]
								a1=0
						for i4 in zhangjie:
							if 'http://www.bxwx9.org' in i4['href']:
								zhangjie_url=i4['href']
							else:
								zhangjie_url=xiaoshuo_url[:-10]  +i4['href']
							try:

								章节(zhangjie_url,i4.text,xiaoshuo_name)

								print(i4.text)
							except Exception as e:
								print(e+'章节目录报错')





					#shuzizhuanhuan(i2.text)
				except Exception as e:
					print(e)



		

	
if __name__ == "__main__":
	print('开始 ')
	url='http://www.bxwx9.org/'
	导航栏(url)
		
